﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using SdmxMl.Manager;

namespace SdmxMl.Common
{
    public enum SdmxVersion
    {
        SDMX_20,
        SDMX_21,
    }

    public class SdmxNamespaceManager : XmlNamespaceManager
    {
        #region SDMX Target

        /// <summary> SDMX Target version for Persistence</summary>
        public static SdmxVersion SdmxTarget { get; set; }

        #endregion
        #region Tags

        static string[] TagArtefactGroup_21 = 
        {
            "",
            "OrganisationSchemes",
            "Dataflows",
            "Metadataflows",
            "CategorySchemes",
            "Categorisations",
            "Codelists",
            "HierarchicalCodelists",
            "Concepts",
            "MetadataStructures",
            "DataStructures",
            "StructureSets",
            "ReportingTaxonomies",
            "Processes",
            "DataSet",
            "MetadataSet"
        };
        static string[] TagArtefact_21 = 
        {
            "",
            "OrganisationScheme",
            "Dataflow",
            "Metadataflow",
            "CategoryScheme",
            "Categorisation",
            "Codelist",
            "HierarchicalCodelist",
            "ConceptScheme",
            "MetadataStructure",
            "DataStructure",
            "StructureSet",
            "ReportingTaxonomy",
            "Process",
            "DataSet",
            "MetadataSet"
        };

        static string[] TagArtefact_20 = 
        {
            "",
            "OrganisationScheme",
            "Dataflow",
            "Metadataflow",
            "CategoryScheme",
            "Categorisation",
            "CodeList",
            "HierarchicalCodelist",
            "ConceptScheme",
            "MetadataStructureDefinition",
            "KeyFamily",
            "StructureSet",
            "ReportingTaxonomy",
            "Process",
            "DataSet",
            "MetadataSet"
        };
        #endregion

        private SdmxVersion versionSdmx;
        public SdmxVersion VersionSdmx
        {
            get { return versionSdmx; }
        }

        public SdmxNamespaceManager(SdmxVersion sdmxVers, XmlNameTable nameTable)
            : base(nameTable)
        {
            versionSdmx = sdmxVers;

            if (versionSdmx == SdmxVersion.SDMX_20)
            {
                AddNamespace("S", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message");
                AddNamespace("message", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message");
                AddNamespace("structure", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure");
                AddNamespace("common", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common");
                AddNamespace("genericmetadata", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/genericmetadata");
                AddNamespace("generic", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic");
                AddNamespace("M", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message");
                AddNamespace("G", "http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic");
            }
            else
            {
                AddNamespace("S", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message");
                AddNamespace("structure", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure");
                AddNamespace("common", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common");
                AddNamespace("genericmetadata", "http://www.SDMX.org/resources/SDMXML/schemas/v2_1/genericmetadata");
            }
        }

        public string GetArtefactTag(SdmxArtefactType arType)
        {
            return TagArtefact_21[(int)arType];
        }

        public string GetArtefactGroupTag(SdmxArtefactType arType)
        {
            return GetArtefactGroupTag(VersionSdmx, arType);
        }

        public static string GetArtefactGroupTag(SdmxVersion v, SdmxArtefactType arType)
        {
            if (v == SdmxVersion.SDMX_20)
                return arType.ToString();
            
            // sdmx 2.1
            return TagArtefactGroup_21[(int)arType];
        }

        public static string GetArtefactTag(SdmxVersion v, SdmxArtefactType arType)
        {
             if (v == SdmxVersion.SDMX_20)
                return TagArtefact_20[(int)arType];

            // Sdmx 2.1
             return TagArtefact_21[(int)arType];
        }

    }
}
